【C语言】蓝桥杯/ACM竞赛入门 A+B for Input 您所在的位置:网站首页 process to end of file 【C语言】蓝桥杯/ACM竞赛入门 A+B for Input

【C语言】蓝桥杯/ACM竞赛入门 A+B for Input

2024-06-02 06:48| 来源: 网络整理| 查看: 265

文章目录 引子A+B for Input-Output Practice (I)在while里使用EOF的原因scanf函数的返回值 A+B for Input-Output Practice (II)A+B for Input-Output Practice (III)A+B for Input-Output Practice (IV)A+B for Input-Output Practice (V)A+B for Input-Output Practice (VI)A+B for Input-Output Practice (VII)A+B for Input-Output Practice (VIII)

引子

上次参加了学校的蓝桥杯校队选拔“集训”

第一次“测试”就直接被考傻了,虽然都是我“好像"学过的内容,但我里里外外真的看不出来到底怎么写,太离谱了!

而且学长用的都是C++,我只学了c语言,后面的题目完全看不懂了。

当然,归根结底还是我太菜了

在这里把我搞了好久终于弄懂的A+B题目分享给大家

开始集训:A+B不有手就行?结束后:我手呢?

注:题目是英文的!!!!

A+B for Input-Output Practice (I)

Problem Description

Your task is to Calculate a + b.

Too easy?! Of course! I specially designed the problem for acm beginners.

You must have found that some problems have the same titles with this one, yes, all these problems were designed for the same aim.

Input

The input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line.

Output

For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.

Sample Input

1 5 10 20

Sample Output

6 30 

这道题看起来非常简单,我一上来就打上了两行scanf,啪的一下就提交了

然后系统啪的一下给我返回了一个大大的红色WRONG ANSWER

正确做法如下:

#include   int main()   {     int a,b;     while(scanf("%d%d",&a,&b)!=EOF) printf("%d\n",a+b);   }   

不能直接使用两行scanf的原因是

题目需要的是这串代码能完整地使用多次,而不是简单的只执行两次

在while里使用EOF的原因

这里使用eof,可以让程序在未输入错误的情况下一直进行循环计算a+b

scanf函数的返回值

scanf函数返回成功读入的数据项数,读入数据时遇到了“文件结束”则返回EOF。

如:scanf("%d %d",&a,&b);

函数返回值为int型。如果a和b都被成功读入,那么scanf的返回值就是2;

如果只有a被成功读入,返回值为1;

如果a和b都未被成功读入,返回值为0;

如果遇到错误或遇到end of file,返回值为EOF。end of file为Ctrl+z 或者Ctrl+d。

[摘自百度知道用户@纵横竖屏的回答]

EOF的值为-1,但不能简单的用-1代替EOF

A+B for Input-Output Practice (II)

Problem Description

Your task is to Calculate a + b.

Input

Input contains an integer N in the first line, and then N lines follow. Each line consists of a pair of integers a and b, separated by a space, one pair of integers per line.

Output

For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.

Sample Input

2 1 5 10 20

Sample Output

6 30

这道题的意思其实就是在键入需要相加的数字之前,先键入需要相加的数字组数(也可以理解为行数)

要求我们用循环的方式完成“相加数字行数”的操作

完成n行后需要跳出该a+b的循环

#include int main() { int a, b; int i, j; int n; scanf("%d", &n);//n即为行数 int sum[20] = { 0 }; for (i = 0; i


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有